package view;

import controller.AdministratorController;
import model.Administrator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class AdministratorView extends JFrame {
    public static boolean flag = true;
    AdministratorController administratorController = new AdministratorController();
    AdministratorView administratorView = null;
    Administrator administrator = null;

    public void loginUi() {
        JFrame jf = new JFrame("管理员登录");
        jf.setIconImage(Toolkit.getDefaultToolkit().createImage(AdministratorView.class.getResource("qq.jpg")));
        JPanel jp = new JPanel();
        JLabel jl1 = new JLabel("管理员登录");
        jl1.setFont(new Font("黑体", Font.BOLD, 24));
        JLabel jl2 = new JLabel("用户名");
        jl2.setFont(new Font("黑体", Font.BOLD, 18));
        JLabel jl3 = new JLabel("密  码");
        jl3.setFont(new Font("黑体", Font.BOLD, 18));
        JTextField jt1 = new JTextField();
        JPasswordField jt2 = new JPasswordField();
        JButton bu1 = new JButton("注册");
        JButton bu2 = new JButton("重置");
        JButton bu3 = new JButton("确定");
        jf.getRootPane().setDefaultButton(bu3);
        JButton bu4 = new JButton("退出");
        jp.setLayout(null);
        jl1.setBounds(200, 1, 200, 50);
        jl2.setBounds(50, 80, 200, 30);
        jl3.setBounds(50, 180, 200, 30);
        jt1.setBounds(150, 80, 200, 30);
        jt2.setBounds(150, 180, 200, 30);
        bu1.setBounds(100, 300, 80, 30);
        bu2.setBounds(100, 400, 80, 30);
        bu3.setBounds(250, 300, 80, 30);
        bu4.setBounds(250, 400, 80, 30);
        jp.add(jl1);
        jp.add(jl2);
        jp.add(jl3);
        jp.add(jt1);
        jp.add(jt2);
        jp.add(bu1);
        jp.add(bu2);
        jp.add(bu3);
        jp.add(bu4);

        jf.add(jp);
        jf.setVisible(true);
        jf.setBounds(600, 200, 500, 500);
        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                jf.dispose();
            }
        });
        bu1.addActionListener(e -> {
            administratorView = new AdministratorView();
            administratorView.addUser();
        });
        bu2.addActionListener(e -> {
            jt1.setText("");
            jt2.setText("");
        });
        bu3.addActionListener(e -> {
            if (!(jt1.getText().replaceAll(" +", "").isEmpty()) && !(jt2.getText().replaceAll(" +", "").isEmpty())) {
                int num;
                String name = jt1.getText();
                String password = jt2.getText();
                num = AdministratorController.login(name, password);
                if (num == -1) {
                    JOptionPane.showMessageDialog(null, "密码不正确");
                }
                if (num == 0) {
                    JOptionPane.showMessageDialog(null, "登录失败");
                }
                if (num == 2) {
                    JOptionPane.showMessageDialog(null, "用户名不存在");
                }
                if (num == 1) {
                    if (flag) {
                        JOptionPane.showMessageDialog(null, "登录成功");
                    } else {
                        JOptionPane.showMessageDialog(null, "管理员已登录！");
                    }
                    jf.dispose();
                    new ChatServer();
                }

            } else {
                JOptionPane.showMessageDialog(null, "用户名和密码不能为空");
            }
        });
        bu4.addActionListener(e -> System.exit(0));

    }

    public void addUser() {
        JFrame jf = new JFrame("管理员注册");
        JPanel jp = new JPanel();
        JLabel jl1 = new JLabel("管理员注册");
        jl1.setFont(new Font("黑体", Font.BOLD, 24));
        JLabel jl2 = new JLabel("用 户 名");
        jl2.setFont(new Font("黑体", Font.BOLD, 18));
        JLabel jl3 = new JLabel("密    码");
        jl3.setFont(new Font("黑体", Font.BOLD, 18));
        JLabel jl4 = new JLabel("确认密码");
        jl4.setFont(new Font("黑体", Font.BOLD, 18));
        JTextField jt1 = new JTextField();//name
        JPasswordField jt2 = new JPasswordField();//pass
        JPasswordField jt3 = new JPasswordField();//repass
        JButton bu1 = new JButton("重置");
        JButton bu2 = new JButton("取消");
        JButton bu3 = new JButton("确定");
        jf.getRootPane().setDefaultButton(bu3);
        jp.setLayout(null);
        jl1.setBounds(200, 1, 200, 50);
        jl2.setBounds(50, 80, 200, 30);
        jl3.setBounds(50, 150, 200, 30);
        jl4.setBounds(50, 220, 200, 30);
        jt1.setBounds(150, 80, 200, 30);
        jt2.setBounds(150, 150, 200, 30);
        jt3.setBounds(150, 220, 200, 30);
        bu1.setBounds(50, 300, 80, 30);
        bu2.setBounds(180, 300, 80, 30);
        bu3.setBounds(300, 300, 80, 30);
        jp.add(jl1);
        jp.add(jl2);
        jp.add(jl3);
        jp.add(jl4);
        jp.add(jt1);
        jp.add(jt2);
        jp.add(jt3);
        jp.add(bu1);
        jp.add(bu2);
        jp.add(bu3);
        jf.add(jp);
        jf.setBounds(600, 200, 500, 500);
        jf.setVisible(true);
        bu1.addActionListener(e -> {
            jt1.setText("");
            jt2.setText("");
            jt3.setText("");
        });
        bu2.addActionListener(
                e -> jf.dispose());
        bu3.addActionListener(e -> {

            String name = jt1.getText().replaceAll(" +", "");
            String pass = jt2.getText().replaceAll(" +", "");
            String repay = jt3.getText().replaceAll(" +", "");
            if ((!(name.isEmpty()) && (!(pass.isEmpty())) && (!(repay.isEmpty())))) {
                if (administratorController.judge(name, pass)) {
                    if (administratorController.judges(name) == 1) {
                        if (pass.equals(repay)) {
                            administrator = new Administrator();
                            administrator.setName(name);
                            administrator.setPassword(pass);
                            administratorController.addUser(administrator);
                            JOptionPane.showMessageDialog(null, "注册成功");
                            jf.dispose();
                        } else {
                            JOptionPane.showMessageDialog(null, "两次输入的密码不相同");
                        }
                    } else {
                        JOptionPane.showMessageDialog(null, "该用户名已存在");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "账号长度必须在1~15且密码长度必须在6~15之间");
                }
            } else {
                JOptionPane.showMessageDialog(null, "用户名或密码全为空格");
            }
        });
    }
}